/*
 * bsp_usart.h
 *
 *  Created on: 2023年6月28日
 *      Author: mcealt_DS
 */

#ifndef BSP_USART_H_
#define BSP_USART_H_


#include "bsp_config.h"
#include "device_type.h"
#include "em_cmu.h"
#include "em_usart.h"
#include "em_eusart.h"
#include "em_gpio.h"

//#define USE_UARTDRV_H   //使用UARTDRV库
#define USE_DEBUG_PRINTF
#ifdef USE_UARTDRV_H
#include "uartdrv.h"
#endif

#include "stdio.h"
#include "circular_buffer.h"
#include "device_type.h" 

/*********************************************************************************************************
** 宏定义
*********************************************************************************************************/

/*
 * usart0 define
 */

#ifdef BSP_ENABLE_USART
#define USART                    USART0
#ifndef GW_DBG_TX_PORT
#define USART_TX_PORT            gpioPortA
#else
#define USART_TX_PORT            GW_DBG_TX_PORT
#endif
#ifndef GW_DBG_TX_PIN
#define USART_TX_PIN             3
#else
#define USART_TX_PIN            GW_DBG_TX_PIN
#endif
#ifndef GW_DBG_RX_PORT
#define USART_RX_PORT            gpioPortA
#else
#define USART_RX_PORT            GW_DBG_RX_PORT
#endif
#ifndef GW_DBG_RX_PIN
#define USART_RX_PIN             4
#else
#define USART_RX_PIN             GW_DBG_RX_PIN
#endif
#define USART_BUFF_SIZE          128
#ifdef CLI_CMD
#define USART_BAUD               115200
#else
#define USART_BAUD               921600
#endif
#define USART_DATABIT            usartDatabits8
#define USART_PARITY             usartNoParity
#define USART_STOPBIT            usartStopbits1
#endif

/*
 * DEBUG define
 */
#ifdef BSP_ENABLE_USART
#define DEBUG_USART              USART0
#else
#define DEBUG_USART              EUSART0
#endif
#define DEBUG_USART_TX_PORT      gpioPortA
#define DEBUG_USART_TX_PIN       3
#define DEBUG_USART_RX_PORT      gpioPortA
#define DEBUG_USART_RX_PIN       4

#define DEBUG_USART_BUFF_SIZE    4096
#define DEBUG_USART_BAUD         115200

#define DEBUG_USART_DATABIT      eusartDataBits8
#define DEBUG_USART_PARITY       eusartNoParity
#define DEBUG_USART_STOPBIT      eusartStopbits1

#ifdef BSP_ENABLE_USART
#define debug_printf    printf
#else
#define debug_printf    EUSART0_PRINTF
#endif
/*
 * COM define
 */
#ifdef BSP_ENABLE_COM
#define COM_USART                EUSART1
#define COM_USART_TX_PORT        GW_COM_TX_PORT
#define COM_USART_TX_PIN         GW_COM_TX_PIN
#define COM_USART_RX_PORT        GW_COM_RX_PORT
#define COM_USART_RX_PIN         GW_COM_RX_PIN
#define COM_USART_BUFF_SIZE      1024

#define COM_USART_DEFAULT_HF                                                                       \
  {                                                                                                   \
    eusartEnable,              /* Enable RX/TX when initialization completed. */                      \
    0,                         /* Use current configured reference clock for configuring baud rate.*/ \
    115200,                    /* 115200 bits/s. */                                                   \
    eusartOVS16,               /* Oversampling x16. */                                                \
    eusartDataBits8,           /* 8 data bits. */                                                     \
    eusartNoParity,            /* No parity. */                                                       \
    eusartStopbits1,           /* 1 stop bit. */                                                      \
    eusartMajorityVoteEnable,  /* Majority vote enabled. */                                           \
    eusartLoopbackDisable,     /* Loop back disabled. */                                              \
    NULL,                      /* Default advanced settings. */                                       \
  }


extern Buffer_t com_TXBuff;
extern Buffer_t com_RXBuff;

//COM USART
void DS_comInit(void);
void DS_com_Txbuff(const char *ptr, int len);

#endif
/*
 * 全局变量定义
 */
extern Buffer_t debug_TXBuff;
extern Buffer_t debug_RXBuff;




/*
 * API name:  DS_usartInit
 * function： usart initialization
 */
void DS_usartInit(void);
/*
 * API name:  DS_usartTxByte
 * function： output a byte data
 *   intput： need output data
 */
void DS_usartTxByte( uint8_t data);
/*
 * 函数名:uartRxByte
 * 功能： 串口接收字节(阻塞方式)
 * 输出： 接收到的字节数据
 */
uint8_t DS_usartRxByte(void);


//DEBUG USART
void DS_debugInit(void);
void DS_debugTxByte(uint8_t Data);
void DS_debugTxbuff(uint8_t *buff, uint16_t size);
uint8_t DS_debugRxByte(void);

/*
 * debug printf
 * debug使用eusart时调用此函数替代printf，否则会丢失前4字节
 */
void EUSART0_PRINTF(const char *format,...);
void debug_printf_hex(uint8_t *buff, uint16_t len);
#endif /* BSP_USART_H_ */
